package cn.itcast.item.service.impl;

import cn.itcast.hmall.dto.common.PageDTO;
import cn.itcast.hmall.dto.common.ResultDTO;
import cn.itcast.hmall.pojo.item.Item;
import cn.itcast.hmall.pojo.item.PageSelectItem;
import cn.itcast.item.constants.MqConstants;
import cn.itcast.item.mapper.ItemMapper;
import cn.itcast.item.service.ItemService;
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.core.toolkit.Wrappers;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import org.apache.commons.lang.StringUtils;
import org.springframework.amqp.rabbit.core.RabbitTemplate;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.List;

@Service
public class ItemServiceImpl extends ServiceImpl<ItemMapper, Item> implements ItemService {
    @Autowired
    private ItemMapper itemMapper;
    @Autowired
    private RabbitTemplate rabbitTemplate;
    @Override
    public PageDTO<Item> pageSelect(PageSelectItem item) {
        IPage<Item> page = new Page(item.getPage(),item.getSize());
        if(StringUtils.isBlank(item.getName())&&(StringUtils.isBlank(item.getBrand()))&&(StringUtils.isBlank(item.getCategory()))){
        itemMapper.selectPage(page,null);
        }else{
            LambdaQueryWrapper<Item> wrapper = Wrappers.<Item>lambdaQuery();
            wrapper.like(Item::getName,item.getName())
                    .eq(Item::getBrand,item.getBrand())
                    .eq(Item::getCategory,item.getCategory())
                    .between(Item::getCreateTime, item.getBeginDate(), item.getEndDate());
            itemMapper.selectPage(page,wrapper);
        }
        List<Item> records = page.getRecords();
        for (Item record : records) {
            Long price = record.getPrice();
            long newprice = price/100;
            record.setPrice(newprice);
        }
        long total = page.getTotal();
        return new PageDTO<>(total,records);
    }

    @Override
    public ResultDTO upDownUpdate(Long id, Integer status) {
        Item item = new Item();
        item.setId(id);
        item.setStatus(status);
        if(item.getStatus()==1){
            rabbitTemplate.convertAndSend(MqConstants.HMALL_EXCHANGE,MqConstants.HMALL_INSERT_KEY,item.getId());
            System.err.println("发送上架信息成功");
        }
        if(item.getStatus()==2) {
            rabbitTemplate.convertAndSend(MqConstants.HMALL_EXCHANGE, MqConstants.HMALL_DELETE_KEY, id);
            System.err.println("发送下架信息成功");
        }
        itemMapper.updateById(item);
        return ResultDTO.ok();
    }

    @Override
    public ResultDTO changeUpdate(Item item) {
        itemMapper.updateById(item);
        return ResultDTO.ok();
    }

    @Override
    public void addUpdate(Item item) {
        Item item1 = itemMapper.selectById(item.getId());
        Integer stock = item1.getStock();
        int reStock = stock+item.getStock();
        item1.setStock(reStock);
        itemMapper.updateById(item1);
    }


}
